package net.malkuth.test;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import net.malkuth.test.fragment.LectureFragment;
import net.malkuth.test.fragment.OtherFragment;
import net.malkuth.test.fragment.QueryFragment;


public class MainActivity extends Activity implements View.OnClickListener {
    /**
     * 用于展示授课的详细内容
     */
    private LectureFragment mlectureFragment;

    /**
     * 用于展示查询的详细内容
     */
    private QueryFragment mqueryFragment;
    /**
     * 用于展示其他的详细内容
     */
    private OtherFragment otherFragment;

    /**
     * 授课界面布局
     */
    private View lecture;
    /**
     * 查询界面布局
     */
    private View query;
    /**
     * 其他页面布局
     */
    private View other;
    /**
     * 用于显示在table上授课文本控件
     */
    private TextView tv_lecture;
    /**
     * 用于显示在table上查询文本控件
     */
    private TextView tv_query;
    /**
     * 用于显示在table上其他文本控件
     */
    private TextView tv_other;
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;

    /**
     * 用于管理授课图片
     */
    private ImageView iv_lecture;
    /**
     * 用于查询授课图片
     */
    private ImageView iv_query;
    /**
     * 用于其他授课图片
     */
    private ImageView iv_other;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_main);
        initView();
        fragmentManager = getFragmentManager();
        setTabSelection(0);

    }

    private void initView() {
        lecture = findViewById(R.id.lecture);
        query = findViewById(R.id.query);
        other = findViewById(R.id.other);

        lecture.setOnClickListener(this);
        query.setOnClickListener(this);
        other.setOnClickListener(this);

        tv_lecture = (TextView) findViewById(R.id.lecture_text);
        tv_query = (TextView) findViewById(R.id.query_text);
        tv_other = (TextView) findViewById(R.id.other_text);

        iv_lecture = (ImageView) findViewById(R.id.lecture_image);
        iv_query = (ImageView) findViewById(R.id.query_image);
        iv_other = (ImageView) findViewById(R.id.other_image);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lecture:
                setTabSelection(0);
                break;
            case R.id.query:
                setTabSelection(1);
                break;
            case R.id.other:
                setTabSelection(2);
                break;
        }
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index 每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                //点击table授课更换字体颜色和图片
                iv_lecture.setImageResource(R.drawable.bg_table_lecture_press);
                tv_lecture.setTextColor(Color.parseColor("#1AB38E"));
                if (mlectureFragment == null) {
                    // LectureFragment为空，则创建一个并添加到界面上
                    mlectureFragment = new LectureFragment();
                    transaction.add(R.id.content, mlectureFragment);
                } else {
                    // LectureFragment，则直接将它显示出来
                    transaction.show(mlectureFragment);
                }
                break;
            case 1:
                //点击table查询更换字体颜色和图片
                iv_query.setImageResource(R.drawable.bg_table_query_press);
                tv_query.setTextColor(Color.parseColor("#1AB38E"));
                if (mqueryFragment == null) {
                    // QueryFragment为空，则创建一个并添加到界面上
                    mqueryFragment = new QueryFragment();
                    transaction.add(R.id.content, mqueryFragment);
                } else {
                    // QueryFragment，则直接将它显示出来
                    transaction.show(mqueryFragment);
                }
                break;
            case 2:
                //点击table其他更换字体颜色和图片
                iv_other.setImageResource(R.drawable.bg_table_other_press);
                tv_other.setTextColor(Color.parseColor("#1AB38E"));
                if (otherFragment == null) {
                    // OtherFragment为空，则创建一个并添加到界面上
                    otherFragment = new OtherFragment();
                    transaction.add(R.id.content, otherFragment);
                } else {
                    // OtherFragment不为空，则直接将它显示出来
                    transaction.show(otherFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void clearSelection() {
        tv_lecture.setTextColor(Color.parseColor("#999999"));
        iv_lecture.setImageResource(R.drawable.bg_table_lecture_normal);
        tv_query.setTextColor(Color.parseColor("#999999"));
        iv_query.setImageResource(R.drawable.bg_table_query_normal);
        tv_other.setTextColor(Color.parseColor("#999999"));
        iv_other.setImageResource(R.drawable.bg_table_other_normal);
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void hideFragments(FragmentTransaction transaction) {
        if (mlectureFragment != null) {
            transaction.hide(mlectureFragment);
        }
        if (mqueryFragment != null) {
            transaction.hide(mqueryFragment);
        }
        if (otherFragment != null) {
            transaction.hide(otherFragment);
        }
    }

}
